package com.mvpandroiddemo.register;


import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;

import androidx.appcompat.widget.AppCompatButton;

import com.google.android.material.textfield.TextInputEditText;
import com.mvpandroiddemo.R;
import com.mvpandroiddemo.base.BaseActivity;
import com.mvpandroiddemo.home.HomeActivity;


import butterknife.BindView;
import butterknife.ButterKnife;
import es.dmoral.toasty.Toasty;

public class RegisterActivity extends BaseActivity implements RegisterControl.IRegisterView {

    @BindView(R.id.tiet_register_user_name)
    TextInputEditText tietRegisterUserName;
    @BindView(R.id.tiet_register_pwd)
    TextInputEditText tietRegisterPwd;
    @BindView(R.id.tiet_register_confirm_pwd)
    TextInputEditText tietRegisterConfirmPwd;
    @BindView(R.id.abtn_register_register)
    AppCompatButton abtnRegisterRegister;
    private RegisterPresent registerPresent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        ButterKnife.bind(this);

        registerPresent = new RegisterPresent();
        registerPresent.attachView(this);
    }


    public void registerViewClick(View view) {
        switch (view.getId()) {
            case R.id.abtn_register_register:
                registerPresent.onRegister();
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (registerPresent != null) {
            registerPresent.detachView();
        }
    }

    @Override
    public String getUserName() {
        Editable text = tietRegisterUserName.getText();
        return text == null ? "" : text.toString();
    }

    @Override
    public String getPwd() {
        Editable text = tietRegisterPwd.getText();
        return text == null ? "" : text.toString();
    }

    @Override
    public String getRePwd() {
        Editable text = tietRegisterConfirmPwd.getText();
        return text == null ? "" : text.toString();
    }

    @Override
    public void registerSuccess() {
        hiddenDialog();
        startActivity(new Intent(this, HomeActivity.class));
        finish();
    }

    @Override
    public void registerFailed(String msg) {
        hiddenDialog();
        Toasty.error(this, "注册失败: " + msg).show();

    }


}
